package cn.group.controller;

import cn.group.domain.Classes;

import cn.group.domain.Student;
import cn.group.service.StudentService;
import cn.group.utils.PinyinUtils;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {

    private StudentService studentService;
    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
    /*跳转到学生管理页面*/
    @GetMapping("/showStudent")
    public String queryAllStudent(){
        return "studentInfoIndex";
    }

    /*获取所有学生*/
    @PostMapping("/getAllStu")
    @ResponseBody
    public Map<String,Object> getAllStudent(Integer currentPage,Integer pageSize,Integer uid){
        //System.out.println("用户id"+uid);
        PageInfo<Student> studentsList = null;
        if(uid == null){
            //教导主任查询全部学生信息
            studentsList = studentService.queryAllStu(currentPage,pageSize);
        }else {
            //班主任查询本班学生信息
            studentsList = studentService.queryStuByTId(currentPage,pageSize,uid);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("stuList",studentsList.getList());
        map.put("totalPage",studentsList.getPages());
        return map;
    }

    /*根据id获取一个学生信息*/
    @PostMapping("/getOneStuById")
    @ResponseBody
    public Map<String,Object> getOneStu(Integer id){
        Map<String,Object> map = studentService.queryOneStuById(id);
        return map;
    }

    /*修改一个学生信息*/
    @PostMapping("/editStu")
    @ResponseBody
    public Map<String,Object> updateStuByid(Integer id,String name ,String birthday,String sex, String address,@RequestParam("class_id") Integer class_id) throws ParseException {
        Student stu = new Student();
        //创建一个classes对象，并封装到student对象中
        Classes clazz = new Classes();
        clazz.setId(class_id);
        stu.setId(id);
        stu.setName(name);
        stu.setAddress(address);
        stu.setSex(sex);
        //将日期字符串转换为日期类，并封装到student对象中
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = sdf.parse(birthday);
        //System.out.println(date);
        stu.setBirthday(date);
        stu.setClasses(clazz);
        Boolean flag = studentService.updateStu(stu);
        Map<String,Object> map = new HashMap<>();
        if(flag){
            map.put("code","0");
        }else {
            map.put("code","-1");
        }
        return map;
    }

    /*删除一个学生*/
    @ResponseBody
    @PostMapping("/deleteStu")
    public Map<String,Object> deleteStu(Integer id){
        Boolean flag = studentService.deleteStu(id);
        Map<String,Object> map = new HashMap<>();
        if(flag){
            map.put("code",0);
        }else {
            map.put("code",-1);
        }
        return map;
    }

    /*增加一个学生*/
    @ResponseBody
    @PostMapping("/addStu")
    public Map<String,Object> addStu(Integer id,String name ,String birthday,String sex, String address,@RequestParam("class_id")  Integer class_id) throws ParseException {
        Student stu = new Student();
        //创建一个classes对象，并封装到student对象中
        Classes clazz = new Classes();
        clazz.setId(class_id);
        stu.setId(id);
        stu.setName(name);
        stu.setAddress(address);
        stu.setSex(sex);
        //将日期字符串转换为日期类，并封装到student对象中
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = sdf.parse(birthday);
        //System.out.println(date);
        stu.setBirthday(date);
        String username = PinyinUtils.getPinYin(name);
        stu.setUsername(username);
        stu.setPassword("123456");
        stu.setClasses(clazz);
        System.out.println(stu);
        boolean flag = studentService.addUser(stu);
        Map<String,Object> map = new HashMap<>();
        if(flag){
            map.put("code","0");
            map.put("message","增加成功");
        }else {
            map.put("code","-1");
            map.put("messge","增加学生失败");
        }
        return map;
    }
    /*模糊查询学生信息*/
    @ResponseBody
    @PostMapping("/searchStu")
    public Map<String,Object> seachStu(Integer currentPage,Integer pageSize,String keyword){
        Map<String,Object> map = new HashMap<>();
        PageInfo<Student> pageInfo = studentService.queryStuByCondition(currentPage,pageSize,keyword);
        map.put("code",0);
        map.put("students",pageInfo.getList());
        map.put("totalPage",pageInfo.getPages());
        return map;
    }

    //获取学生id，name
    @RequestMapping("/someStu")
    @ResponseBody
    public Map<String,Object> getSomeStu(Integer id){
        Student student=(Student)studentService.queryOneStuById(id).get("student");
        System.out.println();
        Map<String,Object> result=new HashMap<>();
        result.put("code",0);
        result.put("student",student);
        result.put("message","获取信息成功");
        System.out.println(result);
        return result;
    }

    /*跳转到展示成绩页面*/
    @GetMapping("/showStuSC")
    public String showSC(){
        return "showSC";
    }

    /*根据学生id获得该学生的所有成绩*/
    /*public Map<String,Object> getSCByStuId(){

    }*/
}
